//
//  BusinessCardViewController.m
//  cm
//
//  Created by suyibaishan on 2017/11/5.
//  Copyright © 2017年 feihu. All rights reserved.
//

#import "BusinessCardViewController.h"

@interface BusinessCardViewController ()
{
    UILabel *nameLabel;
    UIImageView *insideImg;
    UIActivityIndicatorView *indicator;
}
@end

@implementation BusinessCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self reloadData];
    
    [self createBusinessCardView];
}

-(void)reloadData
{
    [[ToolNet sharedManager] getUserInfo:[mUserDefaults objectForKey:@"userCode"] accessToken:[mUserDefaults objectForKey:@"accessToken"] url:MUserCard completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            NSMutableAttributedString *nameStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"我是%@",responseJson[@"data"][@"realName"]]];
            [nameStr addAttribute:NSForegroundColorAttributeName
                            value:[UIColor blackColor]
                            range:NSMakeRange(0, 2)];
            nameLabel.attributedText = nameStr;
            [insideImg sd_setImageWithURL:[NSURL URLWithString:responseJson[@"data"][@"qrCodeUrl"]]];
        }
        else {
            [self showMessage:responseJson[@"msg"]];
        }
    }];
}

-(void)createBusinessCardView
{
    self.titleLabel.text = @"我的名片";
    self.backButton.hidden = NO;

    UIImageView *logoImage = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - 150)/2, kScreenWidth * 0.28f, 150, 80)];
    logoImage.image = [UIImage imageNamed:@"logo_login"];
    [self.view addSubview:logoImage];
    
    nameLabel = [[UILabel alloc] init];
    nameLabel.font = [UIFont systemFontOfSize:18];
    nameLabel.textAlignment = NSTextAlignmentCenter;
//    nameLabel.text = @"我是张良";
    nameLabel.textColor = [UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1];
    [self.view addSubview:nameLabel];
    nameLabel.sd_layout
    .leftSpaceToView(self.view, 50)
    .topSpaceToView(logoImage, 10)
    .widthIs(kScreenWidth - 100)
    .heightIs(25);
    
    UILabel *contactLabel = [[UILabel alloc] init];
    contactLabel.font = [UIFont systemFontOfSize:18];
    contactLabel.textAlignment = NSTextAlignmentCenter;
    contactLabel.textColor = [UIColor blackColor];
    contactLabel.text = @"邀请您加入我的团队";
    [self.view addSubview:contactLabel];
    contactLabel.sd_layout
    .topSpaceToView(nameLabel, 0)
    .leftSpaceToView(self.view, 25)
    .widthIs(kScreenWidth - 50)
    .heightIs(20);

    UIImageView *codeImage = [[UIImageView alloc] init];
    codeImage.image = [UIImage imageNamed:@"QR_code"];
    [self.view addSubview:codeImage];
    codeImage.sd_layout
    .topSpaceToView(contactLabel, 15)
    .leftSpaceToView(self.view, (kScreenWidth - 250)/2)
    .widthIs(250)
    .heightIs(201);
    
    insideImg = [[UIImageView alloc] init];
//    insideImg.contentMode = UIViewContentModeScaleAspectFill;
//    insideImg.clipsToBounds = YES;
    insideImg.userInteractionEnabled = YES;
    [self.view addSubview:insideImg];
    insideImg.sd_layout
    .topSpaceToView(contactLabel, 57.5)
    .leftSpaceToView(self.view, (kScreenWidth - 115)/2)
    .widthIs(115)
    .heightIs(115);
    
    //长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(dealLongPress:)];
    [insideImg addGestureRecognizer:longPress];
    
    UILabel *companyLabel = [[UILabel alloc] init];
    companyLabel.font = [UIFont systemFontOfSize:15];
    companyLabel.numberOfLines = 2;
    companyLabel.textAlignment = NSTextAlignmentCenter;
    companyLabel.textColor = [UIColor blackColor];
    companyLabel.text = @"长按保存二维码\n消费变投资 开启属于你自己的创业团队";
    [self.view addSubview:companyLabel];
    companyLabel.sd_layout
    .topSpaceToView(codeImage, 15)
    .leftSpaceToView(self.view, 25)
    .widthIs(kScreenWidth - 50)
    .heightIs(40);
    
    indicator = [[UIActivityIndicatorView alloc] init];
    indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    indicator.center = self.view.center;
    [self.view addSubview:indicator];
}

-(void)dealLongPress:(UILongPressGestureRecognizer *)longPressGesture
{
    [indicator startAnimating];
    if (longPressGesture.state == UIGestureRecognizerStateBegan) {
        UIImageView *currentImageView = insideImg;
        UIImageWriteToSavedPhotosAlbum(currentImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    }
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
{
    [indicator stopAnimating];
    if (error) {
        [self showMessage:@"保存失败"];
    }
    else {
        [self showMessage:@"保存成功"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
